<!DOCTYPE html>
<html>
<head>
    

    

    
<!-- Baidu Tongji -->
<script>var _hmt = _hmt || []</script>
<script async src="//hm.baidu.com/hm.js?a7c05ce530152d9866930ef4850ee566"></script>
<!-- End Baidu Tongji -->




    <meta charset="utf-8">
    
    
    
    <title>Mybatis快速入门四(映射文件) | 神奇的鸭鸭の码农库 | 新知识要不断的总结记录成笔记，要多写，多画，能够清晰透彻的将知识讲给别人听，才是达到理解的层次。</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#765959">
    
    
    <meta name="keywords" content="SSM">
    <meta name="description" content="Mybatis映射文件MyBatis 的真正强大在于它的映射语句，也是它的魔力所在。由于它的异常强大，映射器的 XML 文件就显得相对简单。如果拿它跟具有相同功能的 JDBC 代码进行对比，你会立即发现省掉了将近 95% 的代码。MyBatis 就是针对 SQL 构建的，并且比普通的方法做的更好。 例如：select 元素有很多属性允许你配置，来决定每条语句的作用细节，了解其他参数请参照官档123">
<meta name="keywords" content="SSM">
<meta property="og:type" content="article">
<meta property="og:title" content="Mybatis快速入门四(映射文件)">
<meta property="og:url" content="http://magic_duck.oschina.io/2017/08/13/Mybatis_primary_04/index.html">
<meta property="og:site_name" content="神奇的鸭鸭の码农库">
<meta property="og:description" content="Mybatis映射文件MyBatis 的真正强大在于它的映射语句，也是它的魔力所在。由于它的异常强大，映射器的 XML 文件就显得相对简单。如果拿它跟具有相同功能的 JDBC 代码进行对比，你会立即发现省掉了将近 95% 的代码。MyBatis 就是针对 SQL 构建的，并且比普通的方法做的更好。 例如：select 元素有很多属性允许你配置，来决定每条语句的作用细节，了解其他参数请参照官档123">
<meta property="og:updated_time" content="2017-12-05T09:58:02.050Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Mybatis快速入门四(映射文件)">
<meta name="twitter:description" content="Mybatis映射文件MyBatis 的真正强大在于它的映射语句，也是它的魔力所在。由于它的异常强大，映射器的 XML 文件就显得相对简单。如果拿它跟具有相同功能的 JDBC 代码进行对比，你会立即发现省掉了将近 95% 的代码。MyBatis 就是针对 SQL 构建的，并且比普通的方法做的更好。 例如：select 元素有很多属性允许你配置，来决定每条语句的作用细节，了解其他参数请参照官档123">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="/css/style.css?v=1.4.3">
    <script>window.lazyScripts=[]</script>
</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">神奇的鸭鸭</h5>
          <a href="mailto:702038338@qq.com" title="702038338@qq.com" class="mail">702038338@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                归档
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://git.oschina.net/magic_duck" target="_blank" >
                <i class="icon icon-lg icon-gg-circle"></i>
                码云
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://www.zhihu.com/people/shen-qi-de-ya-ya" target="_blank" >
                <i class="icon icon-lg icon-twitter"></i>
                知乎
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/404.html"  >
                <i class="icon icon-lg icon-link"></i>
                404页面测试
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">Mybatis快速入门四(映射文件)</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">Mybatis快速入门四(映射文件)</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-08-13T12:55:00.000Z" itemprop="datePublished" class="page-time">
  2017-08-13
</time>


            
        </h5>
    </div>

    

</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#Mybatis映射文件"><span class="post-toc-number">1.</span> <span class="post-toc-text">Mybatis映射文件</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#输入映射-ParameterType"><span class="post-toc-number">2.</span> <span class="post-toc-text">输入映射(ParameterType)</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#传递简单类型"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">传递简单类型</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#传递POJO对象"><span class="post-toc-number">2.2.</span> <span class="post-toc-text">传递POJO对象</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#传递POJO包装对象"><span class="post-toc-number">2.3.</span> <span class="post-toc-text">传递POJO包装对象</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#传递HashMap"><span class="post-toc-number">2.4.</span> <span class="post-toc-text">传递HashMap</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#输出映射"><span class="post-toc-number">3.</span> <span class="post-toc-text">输出映射</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#输出映射-resultType"><span class="post-toc-number">4.</span> <span class="post-toc-text">输出映射(resultType)</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#resultMap"><span class="post-toc-number">5.</span> <span class="post-toc-text">resultMap</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#动态SQL"><span class="post-toc-number">6.</span> <span class="post-toc-text">动态SQL</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#if，where"><span class="post-toc-number">6.1.</span> <span class="post-toc-text">if，where</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#choose-when-otherwise"><span class="post-toc-number">6.2.</span> <span class="post-toc-text">choose,when,otherwise</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#trim-set"><span class="post-toc-number">6.3.</span> <span class="post-toc-text">trim,set</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#foreach"><span class="post-toc-number">6.4.</span> <span class="post-toc-text">foreach</span></a></li></ol></li></ol>
        </nav>
    </aside>
    
<article id="post-Mybatis_primary_04"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">Mybatis快速入门四(映射文件)</h1>
        <div class="post-meta">
            <time class="post-time" title="2017年08月13日 20:55" datetime="2017-08-13T12:55:00.000Z"  itemprop="datePublished">2017-08-13</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h2 id="Mybatis映射文件"><a href="#Mybatis映射文件" class="headerlink" title="Mybatis映射文件"></a>Mybatis映射文件</h2><p>MyBatis 的真正强大在于它的映射语句，也是它的魔力所在。由于它的异常强大，映射器的 XML 文件就显得相对简单。如果拿它跟具有相同功能的 JDBC 代码进行对比，你会立即发现省掉了将近 95% 的代码。MyBatis 就是针对 SQL 构建的，并且比普通的方法做的更好。</p>
<p>例如：select 元素有很多属性允许你配置，来决定每条语句的作用细节，了解其他参数请参照<a href="http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#" target="_blank" rel="external">官档</a><br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- select：表示一个MappedStatement对象 --&gt;</span></div><div class="line"><span class="comment">&lt;!-- id：代表statement的唯一标示，可以用来引用这条语句 --&gt;</span></div><div class="line"><span class="comment">&lt;!-- parameterType(可选配置，默认mabatis自动选择)：输入参数的java类型(可以写全限定类名) --&gt;</span></div><div class="line"><span class="comment">&lt;!-- resultType(和resultMap二选一)：输出结果所映射的java类型(单条结果所对应的java类型) --&gt;</span></div><div class="line"><span class="comment">&lt;!-- resultMap(和resultType二选一)：可以进行高级结果映射(一对一，一对多映射) --&gt;</span></div><div class="line"><span class="comment">&lt;!-- flushCache(可选配置)：如果设置true可以刷新当前select语句的二级缓存，默认是false --&gt;</span></div><div class="line"><span class="comment">&lt;!-- useCache(可选配置)： 如果设置false，可以禁用当前select语句的二级缓存，即每次查询都是去数据库查，默认时true --&gt;</span></div><div class="line"><span class="comment">&lt;!-- timeout(可选配置)：在抛出异常之前，驱动程序等待数据库返回请求结果的秒数。默认值为unset(依赖驱动) --&gt;</span></div><div class="line"><span class="comment">&lt;!-- fetchSize(可选配置)：这是尝试影响驱动程序每次批量返回的结果行数和这个设置值相等。默认值为unset(依赖驱动) --&gt;</span></div><div class="line"><span class="comment">&lt;!-- statementType(可选配置)：设置MyBatis分别使用：STATEMENT，PREPARED(默认)，CALLABLE --&gt;</span></div><div class="line"><span class="comment">&lt;!-- resultSetType(可选配置)：设置FORWARD_ONLY：结果集的游标只能向下滚动</span></div><div class="line"><span class="comment">                                SCROLL_SENSITIVE：结果集的游标可以上下移动，当数据库变化时，当前结果集不变</span></div><div class="line"><span class="comment">                                SCROLL_INSENSITIVE：返回可滚动的结果集，当数据库变化时，当前结果集同步改变。</span></div><div class="line"><span class="comment">                                中的一个。默认值为unset(依赖驱动) --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">select</span></span></div><div class="line"><span class="tag">  <span class="attr">id</span>=<span class="string">"selectPerson"</span></span></div><div class="line"><span class="tag">  <span class="attr">parameterType</span>=<span class="string">"int"</span></span></div><div class="line"><span class="tag">  <span class="attr">parameterMap</span>=<span class="string">"deprecated"</span></span></div><div class="line"><span class="tag">  <span class="attr">resultType</span>=<span class="string">"hashmap"</span></span></div><div class="line"><span class="tag">  <span class="attr">resultMap</span>=<span class="string">"personResultMap"</span></span></div><div class="line"><span class="tag">  <span class="attr">flushCache</span>=<span class="string">"false"</span></span></div><div class="line"><span class="tag">  <span class="attr">useCache</span>=<span class="string">"true"</span></span></div><div class="line"><span class="tag">  <span class="attr">timeout</span>=<span class="string">"10000"</span></span></div><div class="line"><span class="tag">  <span class="attr">fetchSize</span>=<span class="string">"256"</span></span></div><div class="line"><span class="tag">  <span class="attr">statementType</span>=<span class="string">"PREPARED"</span></span></div><div class="line"><span class="tag">  <span class="attr">resultSetType</span>=<span class="string">"FORWARD_ONLY"</span>&gt;</span></div></pre></td></tr></table></figure></p>
<hr>
<h2 id="输入映射-ParameterType"><a href="#输入映射-ParameterType" class="headerlink" title="输入映射(ParameterType)"></a>输入映射(ParameterType)</h2><p>指定输入参数的 java 类型，可以使用别名或者全限定类名。它可以接收简单类型、POJO、HashMap，下面介绍他们的使用技巧。</p>
<h3 id="传递简单类型"><a href="#传递简单类型" class="headerlink" title="传递简单类型"></a>传递简单类型</h3><p>参考入门一：根据用户ID查询用户信息<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findUserById"</span> <span class="attr">parameterType</span>=<span class="string">"int"</span> <span class="attr">resultType</span>=<span class="string">"cn.sqdyy.mybatis.dao.User"</span>&gt;</span></div><div class="line">  select * from user where id = #&#123;id&#125;;</div><div class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h3 id="传递POJO对象"><a href="#传递POJO对象" class="headerlink" title="传递POJO对象"></a>传递POJO对象</h3><p>传递简单POJO(参考入门一：添加用户)<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">"insertUser"</span> <span class="attr">parameterType</span>=<span class="string">"cn.sqdyy.mybatis.dao.User"</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">selectKey</span> <span class="attr">keyProperty</span>=<span class="string">"id"</span> <span class="attr">resultType</span>=<span class="string">"int"</span> <span class="attr">order</span>=<span class="string">"AFTER"</span>&gt;</span></div><div class="line">    select last_insert_id();</div><div class="line">  <span class="tag">&lt;/<span class="name">selectKey</span>&gt;</span></div><div class="line">  insert into user</div><div class="line">  (username,birthday,sex,address)</div><div class="line">  values(#&#123;username&#125;,#&#123;birthday&#125;,#&#123;sex&#125;,#&#123;address&#125;);</div><div class="line"><span class="tag">&lt;/<span class="name">insert</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h3 id="传递POJO包装对象"><a href="#传递POJO包装对象" class="headerlink" title="传递POJO包装对象"></a>传递POJO包装对象</h3><p>开发中通过POJO传递查询条件，查询条件是综合的查询结果，不仅包括用户查询条件，还会包括其他的查询条件，这时候可以使用包装对象传递输入参数。</p>
<p>1 创建扩展类<br>一般User.java类要和数据表字段一致，所以最好不要在这里添加字段，所以针对要扩展的po类，我们需要创建一个扩展类来继承它：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserExt</span> <span class="keyword">extends</span> <span class="title">User</span> </span>&#123;</div><div class="line">    <span class="comment">// 这里我们可以定义User的一些扩展信息</span></div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>2 定义POJO包装类<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserQueryVO</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="comment">// 这里可以把复杂查询的其他条件加上</span></div><div class="line">	</div><div class="line">    <span class="comment">// 用户信息</span></div><div class="line">    <span class="keyword">private</span> UserExt userExt;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> UserExt <span class="title">getUserExt</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> userExt;</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setUserExt</span><span class="params">(UserExt userExt)</span> </span>&#123;</div><div class="line">    	<span class="keyword">this</span>.userExt = userExt;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>3 编写Mapper接口<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// 通过包装类来进行复杂的用户信息综合查询</span></div><div class="line"><span class="function"><span class="keyword">public</span> List&lt;UserExt&gt; <span class="title">findUserList</span><span class="params">(UserQueryVO userQueryVO)</span></span>;</div></pre></td></tr></table></figure></p>
<p>4 编写Mapper映射<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 通过包装类来进行复杂的用户信息综合查询 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findUserList"</span> <span class="attr">parameterType</span>=<span class="string">"cn.sqdyy.mybatis.dao.UserQueryVO"</span> <span class="attr">resultType</span>=<span class="string">"cn.sqdyy.mybatis.dao.UserExt"</span>&gt;</span></div><div class="line">  select * from user where </div><div class="line">  sex=#&#123;userExt.sex&#125; and </div><div class="line">  username like '%$&#123;userExt.username&#125;%'</div><div class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>5 编写测试<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Test</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">findUserListTest</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">    <span class="comment">// 创建SqlSession</span></div><div class="line">    SqlSession sqlSession = sqlSessionFactory.openSession();</div><div class="line">    <span class="comment">// 通过sqlSession，获取mapper接口的动态代理对象</span></div><div class="line">    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);</div><div class="line">    UserQueryVO userQueryVO = <span class="keyword">new</span> UserQueryVO();</div><div class="line">    UserExt userExt = <span class="keyword">new</span> UserExt();</div><div class="line">    userExt.setSex(<span class="string">"1"</span>);</div><div class="line">    userExt.setUsername(<span class="string">"小明"</span>);</div><div class="line">    userQueryVO.setUserExt(userExt);</div><div class="line">    <span class="comment">// 调用mapper对象的方法</span></div><div class="line">    List&lt;UserExt&gt; list = userMapper.findUserList(userQueryVO);</div><div class="line">    System.out.println(list);</div><div class="line">    <span class="comment">// 关闭资源</span></div><div class="line">    sqlSession.close();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h3 id="传递HashMap"><a href="#传递HashMap" class="headerlink" title="传递HashMap"></a>传递HashMap</h3><p>同传递POJO对象一样，map的key相当于POJO的属性：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 传递hashmap综合查询用户信息 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findUserByHashMap"</span> <span class="attr">parameterType</span>=<span class="string">"hashmap"</span> <span class="attr">resultType</span>=<span class="string">"cn.sqdyy.mybatis.dao.User"</span>&gt;</span></div><div class="line">  select * from user where id=#&#123;id&#125; and username like '%$&#123;username&#125;%'</div><div class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>测试(传递的map中key和sql中解析的key不一致，会抛出异常)：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">	<span class="meta">@Test</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">findUserByHashMapTest</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">    <span class="comment">// 创建SqlSession</span></div><div class="line">    SqlSession sqlSession = sqlSessionFactory.openSession();</div><div class="line">    <span class="comment">// 通过sqlSession，获取mapper接口的动态代理对象</span></div><div class="line">    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);</div><div class="line">    <span class="comment">// 调用mapper对象的方法</span></div><div class="line">    HashMap&lt;String,Object&gt; hashmap = <span class="keyword">new</span> HashMap&lt;&gt;();</div><div class="line">    hashmap.put(<span class="string">"id"</span>, <span class="number">1</span>);</div><div class="line">    hashmap.put(<span class="string">"username"</span>, <span class="string">"管理员"</span>);</div><div class="line">    List&lt;User&gt; list = userMapper.findUserByHashMap(hashmap);</div><div class="line">    System.out.println(list);</div><div class="line">    <span class="comment">// 关闭资源</span></div><div class="line">    sqlSession.close();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<hr>
<h2 id="输出映射"><a href="#输出映射" class="headerlink" title="输出映射"></a>输出映射</h2><p>MyBatis中在查询进行select映射的时候，返回类型可以用resultType，也可以用resultMap，resultType是直接表示返回类型的，而resultMap则是对外部ResultMap的引用，但是resultType跟resultMap不能同时存在。</p>
<h2 id="输出映射-resultType"><a href="#输出映射-resultType" class="headerlink" title="输出映射(resultType)"></a>输出映射(resultType)</h2><p>使用resultType进行结果映射时，查询的列名和映射的POJO属性名完全一致，该列才会映射成功。 <strong>当输出结果只有一列时，可以使用ResultType指定简单类型作为输出结果类型</strong></p>
<ul>
<li>如果查询的列名和映射的POJO属性名全部不一致，则不会创建POJO对象。</li>
<li>如果查询的列名和映射的POJO属性名有一个一致，就会创建POJO对象。</li>
</ul>
<p>例如需求：综合查询用户总数量，需要传入查询条件复杂：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 综合查询用户总数量，需要传入查询条件复杂 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findUsersCount"</span> <span class="attr">parameterType</span>=<span class="string">"cn.sqdyy.mybatis.dao.UserQueryVO"</span> <span class="attr">resultType</span>=<span class="string">"int"</span>&gt;</span></div><div class="line">  select count(*) from user where sex=#&#123;userExt.sex&#125; and username like '%$&#123;userExt.username&#125;%'</div><div class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>编写测试：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Test</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">findUsersCountTest</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">    <span class="comment">// 创建SqlSession</span></div><div class="line">    SqlSession sqlSession = sqlSessionFactory.openSession();</div><div class="line">    <span class="comment">// 通过sqlSession，获取mapper接口的动态代理对象</span></div><div class="line">    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);</div><div class="line">    UserQueryVO userQueryVO = <span class="keyword">new</span> UserQueryVO();</div><div class="line">    UserExt userExt = <span class="keyword">new</span> UserExt();</div><div class="line">    userExt.setSex(<span class="string">"1"</span>);</div><div class="line">    userExt.setUsername(<span class="string">"小明"</span>);</div><div class="line">    userQueryVO.setUserExt(userExt);</div><div class="line">    <span class="comment">// 调用mapper对象的方法</span></div><div class="line">    <span class="keyword">int</span> count = userMapper.findUsersCount(userQueryVO);</div><div class="line">    System.out.println(count);</div><div class="line">    <span class="comment">// 关闭资源</span></div><div class="line">    sqlSession.close();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>注意：输出单个POJO对象和POJO列表[POJO对象]时，mapper映射中的resultType的类型时一样的，mapper接口的方法返回值不同。同样的mapper映射文件，返回单个对象和对象列表时，mapper接口在生成动态代理时，会根据返回值的类型，决定调用selectOne方法还是selectList方法。</p>
<hr>
<h2 id="resultMap"><a href="#resultMap" class="headerlink" title="resultMap"></a>resultMap</h2><p>resultMap可以进行高级结果映射(一对一，一对多映射)，假设我们的需求中，如果查询出来的列名和属性名不一致，就需要通过定义一个resultMap将列名和pojo属性名直接作一个映射关系，基本步骤如下：</p>
<ul>
<li>1 定义resultMap</li>
<li>2 使用resultMap作为statement的输出映射类型。</li>
</ul>
<p>如下面需求，把下面SQL的输出结果集进行映射：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- select id id_, username username_, sex sex_ from user where id=#&#123;id&#125; --&gt;</span></div><div class="line"><span class="comment">&lt;!-- 定义resultMap --&gt;</span></div><div class="line"><span class="comment">&lt;!-- id：定义resultMap的唯一标识 --&gt;</span></div><div class="line"><span class="comment">&lt;!-- type：定义该resultMap最终映射的pojo对象 --&gt;</span></div><div class="line"><span class="comment">&lt;!-- id标签：映射结果集的唯一标识，如果是多个字段联合唯一，则定义多个id标签 --&gt;</span></div><div class="line"><span class="comment">&lt;!-- result标签：映射结果集的普通列</span></div><div class="line"><span class="comment">            column：sql查询的列名，如果有别名则填别名</span></div><div class="line"><span class="comment">            property：pojo对象的属性名 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">type</span>=<span class="string">"cn.sqdyy.mybatis.dao.User"</span> <span class="attr">id</span>=<span class="string">"userResultMap"</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">id</span> <span class="attr">column</span>=<span class="string">"id_"</span> <span class="attr">property</span>=<span class="string">"id"</span>/&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"username_"</span> <span class="attr">property</span>=<span class="string">"username"</span>/&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">result</span> <span class="attr">column</span>=<span class="string">"sex_"</span> <span class="attr">property</span>=<span class="string">"sex"</span>/&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></div><div class="line"><span class="comment">&lt;!-- 根据ID查询用户学习(学习resultMap) --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findUserByIdResultMap"</span> <span class="attr">parameterType</span>=<span class="string">"int"</span> <span class="attr">resultMap</span>=<span class="string">"userResultMap"</span>&gt;</span></div><div class="line">  select id id_, username username_, sex sex_</div><div class="line">  from user where id=#&#123;id&#125;</div><div class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>编写测试：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Test</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">findUserByIdResultMapTest</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">    <span class="comment">// 创建SqlSession</span></div><div class="line">    SqlSession sqlSession = sqlSessionFactory.openSession();</div><div class="line">    <span class="comment">// 通过sqlSession，获取mapper接口的动态代理对象</span></div><div class="line">    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);</div><div class="line">    <span class="comment">// 调用mapper对象的方法</span></div><div class="line">    User user = userMapper.findUserByIdResultMap(<span class="number">1</span>);</div><div class="line">    System.out.println(user);</div><div class="line">    <span class="comment">// 关闭资源</span></div><div class="line">    sqlSession.close();</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<hr>
<h2 id="动态SQL"><a href="#动态SQL" class="headerlink" title="动态SQL"></a>动态SQL</h2><p>通过Mybatis提供的各种动态标签实现动态拼接sql，使得mapper映射文件在编写SQL时更加灵活，方便。常用的动态SQL有：if,where,foreach;</p>
<h3 id="if，where"><a href="#if，where" class="headerlink" title="if，where"></a>if，where</h3><ul>
<li>if标签作为判断入参来使用的，如果符合条件，则把if标签体内的sql拼接上。 <strong>用if进行判断是否为空时，不仅要判断null，也要判断空字符串</strong></li>
<li>where 元素知道只有在一个以上的if条件有值的情况下才去插入“WHERE”子句。而且，若最后的内容是“AND”或“OR”开头的，where 元素也知道如何将他们去除。</li>
</ul>
<p>我们改造前面的findUserList使他更灵活的进行多条件查询：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 通过包装类来进行复杂的用户信息综合查询 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findUserList"</span> <span class="attr">parameterType</span>=<span class="string">"cn.sqdyy.mybatis.dao.UserQueryVO"</span> <span class="attr">resultType</span>=<span class="string">"cn.sqdyy.mybatis.dao.UserExt"</span>&gt;</span></div><div class="line">    select * from user</div><div class="line">    <span class="comment">&lt;!-- where标签：默认去掉后面第一个and，如果没有参数，则把自己去掉 --&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">where</span>&gt;</span></div><div class="line">      <span class="comment">&lt;!-- if标签：可以对输入的参数解析判断，test可以指定表达式 --&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">"userExt != null"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">"userExt.username != null and userExt.username != ''"</span>&gt;</span></div><div class="line">          and username like '%$&#123;userExt.username&#125;%'</div><div class="line">        <span class="tag">&lt;/<span class="name">if</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">"userExt.sex != null and userExt.sex != ''"</span>&gt;</span></div><div class="line">          and sex = #&#123;userExt.sex&#125;</div><div class="line">        <span class="tag">&lt;/<span class="name">if</span>&gt;</span></div><div class="line">      <span class="tag">&lt;/<span class="name">if</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">where</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h3 id="choose-when-otherwise"><a href="#choose-when-otherwise" class="headerlink" title="choose,when,otherwise"></a>choose,when,otherwise</h3><p>有些时候，我们不想用到所有的条件语句，而只想从中择其一二。针对这种情况，MyBatis提供了choose元素，它有点像Java中的switch语句。</p>
<ul>
<li>choose -&gt; switch</li>
<li>when -&gt; case 并且自带barck;</li>
<li>otherwise -&gt; default</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="comment">&lt;!-- 如果id!=null直接查id，否则往下查如果username!=null直接查username，如果两者都没有查所有 --&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findUserByChoose"</span> <span class="attr">parameterType</span>=<span class="string">"cn.sqdyy.mybatis.dao.User"</span> <span class="attr">resultType</span>=<span class="string">"cn.sqdyy.mybatis.dao.User"</span>&gt;</span></div><div class="line">  select * from user</div><div class="line">  <span class="tag">&lt;<span class="name">where</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">choose</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">when</span> <span class="attr">test</span>=<span class="string">"id != null and id != ''"</span>&gt;</span></div><div class="line">        and id = #&#123;id&#125;</div><div class="line">      <span class="tag">&lt;/<span class="name">when</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">when</span> <span class="attr">test</span>=<span class="string">"username != null and username != ''"</span>&gt;</span></div><div class="line">        and username like '%$&#123;username&#125;%'</div><div class="line">      <span class="tag">&lt;/<span class="name">when</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">otherwise</span>&gt;</span></div><div class="line">        and 1=1</div><div class="line">      <span class="tag">&lt;/<span class="name">otherwise</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">choose</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">where</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></div></pre></td></tr></table></figure>
<h3 id="trim-set"><a href="#trim-set" class="headerlink" title="trim,set"></a>trim,set</h3><p>如果where没有按正常套路出牌，你也可以通过自定义trim元素来定制想要的功能，例如：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">trim</span> <span class="attr">prefix</span>=<span class="string">"where"</span> <span class="attr">prefixOverrides</span>=<span class="string">"and | or"</span>&gt;</span><span class="tag">&lt;/<span class="name">trim</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>类似的用于动态更新语句的解决方案是set。可以被用于动态包含需要更新的列，而舍去其他的，比如：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">update</span> <span class="attr">id</span>=<span class="string">"updateUser"</span> <span class="attr">parameterType</span>=<span class="string">"cn.sqdyy.mybatis.dao.User"</span>&gt;</span></div><div class="line">  update user</div><div class="line">  <span class="tag">&lt;<span class="name">set</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">"username != null"</span>&gt;</span>username=#&#123;username&#125;,<span class="tag">&lt;/<span class="name">if</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">"sex != null"</span>&gt;</span>sex=#&#123;sex&#125;<span class="tag">&lt;/<span class="name">if</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">set</span>&gt;</span></div><div class="line">  where id=#&#123;id&#125;</div><div class="line"><span class="tag">&lt;/<span class="name">update</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h3 id="foreach"><a href="#foreach" class="headerlink" title="foreach"></a>foreach</h3><p>动态 SQL 的另外一个常用的必要操作是需要对一个集合进行遍历，通常是在构建 IN 条件语句的时候。</p>
<p>在查询用户总数的statement中增加多个id输入查询，如：select * from user where id in(1,2)：<br>在UserQueryVO中添加新字段：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// id集合</span></div><div class="line"><span class="keyword">private</span> List&lt;Integer&gt; idList;</div></pre></td></tr></table></figure></p>
<p>编写映射文件：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"findUsersCount"</span> <span class="attr">parameterType</span>=<span class="string">"cn.sqdyy.mybatis.dao.UserQueryVO"</span> <span class="attr">resultType</span>=<span class="string">"int"</span>&gt;</span></div><div class="line">    select count(*) from user where 1=1 </div><div class="line">    <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">"idList != null and idList.size &gt; 0"</span>&gt;</span></div><div class="line">      <span class="comment">&lt;!-- foreach标签：表示一个foreach循环 --&gt;</span></div><div class="line">      <span class="comment">&lt;!-- collection：集合参数的名称，如果时直接传入集合参数，则该处的参数名称只能填[list] --&gt;</span></div><div class="line">      <span class="comment">&lt;!-- item：每次遍历出来的对象 --&gt;</span></div><div class="line">      <span class="comment">&lt;!-- open：开始遍历时拼接的串 --&gt;</span></div><div class="line">      <span class="comment">&lt;!-- close：结束遍历时拼接的串 --&gt;</span></div><div class="line">      <span class="comment">&lt;!-- separator：遍历出的每个对象直接需要拼接的字符 --&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">foreach</span> <span class="attr">collection</span>=<span class="string">"idList"</span> <span class="attr">item</span>=<span class="string">"id"</span> <span class="attr">open</span>=<span class="string">"and id in("</span> <span class="attr">separator</span>=<span class="string">","</span> <span class="attr">close</span>=<span class="string">")"</span>&gt;</span></div><div class="line">        #&#123;id&#125;</div><div class="line">      <span class="tag">&lt;/<span class="name">foreach</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">if</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>编写测试：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Test</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">findUsersCountTest</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">    <span class="comment">// 创建SqlSession</span></div><div class="line">    SqlSession sqlSession = sqlSessionFactory.openSession();</div><div class="line">    <span class="comment">// 通过sqlSession，获取mapper接口的动态代理对象</span></div><div class="line">    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);</div><div class="line">    UserQueryVO userQueryVO = <span class="keyword">new</span> UserQueryVO();</div><div class="line">    List&lt;Integer&gt; idList = <span class="keyword">new</span> ArrayList&lt;&gt;();</div><div class="line">    idList.add(<span class="number">1</span>);</div><div class="line">    idList.add(<span class="number">2</span>);</div><div class="line">  userQueryVO.setIdList(idList);</div><div class="line">    <span class="comment">// 调用mapper对象的方法</span></div><div class="line">    <span class="keyword">int</span> count = userMapper.findUsersCount(userQueryVO);</div><div class="line">    System.out.println(count);</div><div class="line">    <span class="comment">// 关闭资源</span></div><div class="line">    sqlSession.close();</div><div class="line">&#125;</div><div class="line"><span class="comment">// DEBUG [main] - ==&gt;  Preparing: select count(*) from user where 1=1 and id in( ? , ? ) </span></div><div class="line"><span class="comment">// DEBUG [main] - ==&gt; Parameters: 1(Integer), 2(Integer)</span></div></pre></td></tr></table></figure></p>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-12-05T09:58:02.050Z" itemprop="dateUpdated">2017年12月5日 17:58</time>
</span><br>


        如要转载请注明出处：<a href="/2017/08/13/Mybatis_primary_04/" target="_blank" rel="external">http://magic_duck.oschina.io/2017/08/13/Mybatis_primary_04/</a>
    </div>
    <footer>
        <a href="http://magic_duck.oschina.io">
            <img src="/img/avatar.jpg" alt="神奇的鸭鸭">
            神奇的鸭鸭
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/SSM/">SSM</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://magic_duck.oschina.io/2017/08/13/Mybatis_primary_04/&title=《Mybatis快速入门四(映射文件)》 — 神奇的鸭鸭の码农库&pic=http://magic_duck.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://magic_duck.oschina.io/2017/08/13/Mybatis_primary_04/&title=《Mybatis快速入门四(映射文件)》 — 神奇的鸭鸭の码农库&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://magic_duck.oschina.io/2017/08/13/Mybatis_primary_04/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Mybatis快速入门四(映射文件)》 — 神奇的鸭鸭の码农库&url=http://magic_duck.oschina.io/2017/08/13/Mybatis_primary_04/&via=http://magic_duck.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://magic_duck.oschina.io/2017/08/13/Mybatis_primary_04/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/08/14/Mybatis_primary_05/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">Mybatis快速入门五(关联查询映射)</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/08/13/Mybatis_primary_03/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">Mybatis快速入门三(全局配置)</h4>
      </a>
    </div>
  
</nav>



    







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        我只要一角钱~ ~
        <i class="icon icon-quote-right"></i>
    </h3>
    <ul class="reward-items">
        
        <li>
            <img src="/img/wechat.png" title="微信打赏二维码" alt="微信打赏二维码">
            <p>微信</p>
        </li>
        

        
        <li>
            <img src="/img/alipay.jpg" title="支付宝打赏二维码" alt="支付宝打赏二维码">
            <p>支付宝</p>
        </li>
        
    </ul>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            <span><a href="" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            <span>博客内容遵循 <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p>
            <span>Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://git.oschina.net/z77z" target="_blank">邹海清</a></span>
            <span>神奇的鸭鸭の码农库 &copy; 2017</span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://magic_duck.oschina.io/2017/08/13/Mybatis_primary_04/&title=《Mybatis快速入门四(映射文件)》 — 神奇的鸭鸭の码农库&pic=http://magic_duck.oschina.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://magic_duck.oschina.io/2017/08/13/Mybatis_primary_04/&title=《Mybatis快速入门四(映射文件)》 — 神奇的鸭鸭の码农库&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://magic_duck.oschina.io/2017/08/13/Mybatis_primary_04/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Mybatis快速入门四(映射文件)》 — 神奇的鸭鸭の码农库&url=http://magic_duck.oschina.io/2017/08/13/Mybatis_primary_04/&via=http://magic_duck.oschina.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://magic_duck.oschina.io/2017/08/13/Mybatis_primary_04/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=http://magic_duck.oschina.io/2017/08/13/Mybatis_primary_04/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };



lazyScripts.push('//s95.cnzz.com/z_stat.php?id=1261081671&web_id=1261081671')

</script>

<script src="/js/main.min.js?v=1.4.3"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="/js/search.min.js?v=1.4.3" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>


</body>
</html>
